package com.stars.controller.system;

import com.github.pagehelper.PageInfo;
import com.stars.common.MyStrRegex;
import com.stars.common.annotation.Log;
import com.stars.common.base.BaseMsg;
import com.stars.common.base.ResultMsg;
import com.stars.common.enums.OperationType;
import com.stars.common.valid.AddView;
import com.stars.common.valid.UpdateView;
import com.stars.controller.BaseController;
import com.stars.entity.dto.system.OperationLogDTO;
import com.stars.entity.system.OperationLogEntity;
import com.stars.services.system.OperationLogAccessServices;
import com.stars.services.system.OperationLogConfigServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: 系统日志 mybatis dao
 * @author dj
 * @date 2019-10-09 19:31:24
 * @version V1.0
 *
 */
@Slf4j
@RestController
@RequestMapping("/operationLogCtl")
@Api(tags ="系统日志")
public class OperationLogCtl extends BaseController{

   @Autowired
   private OperationLogAccessServices  operationLogAccessServicesImpl;
   @Autowired
   private OperationLogConfigServices  operationLogConfigServicesImpl;
   private static final String COLUMNS_NAME ="operation_type";
   private static final String TABLE_NAME ="system_operation_log";
   private static final String SUM_COL ="run_time";



   /**
   *  分页查询
   * @param pageNum 起始页
   * @param pageSize 大小
   * @return 分页数据
   * @throws Exception
   */
   @GetMapping("/pageList")
   @ApiOperation(value = "系统日志"+BaseMsg.MODULAR_PAGE,notes ="系统日志"+ BaseMsg.MODULAR_PAGE)
   @Log(detail = "系统日志"+ BaseMsg.MODULAR_PAGE,operationType = OperationType.SELECT)
   public PageInfo<OperationLogDTO> pageOperationLogList(Integer pageNum, Integer pageSize, OperationLogDTO dto,HttpServletRequest request) throws Exception{

        return operationLogAccessServicesImpl.pageList(pageNum,pageSize,dto);

   }


    /**
    * 保存
    * @param entityName
    * @return 成功失败
    * @throws Exception
    */
    @PostMapping(value = "/add")
    @ApiOperation(value = "系统日志"+ BaseMsg.MODULAR_ADD,notes = "系统日志"+BaseMsg.MODULAR_ADD)
    @Log(detail = "系统日志"+BaseMsg.MODULAR_ADD,operationType = OperationType.INSERT)
    public ResultMsg addOperationLog(@Validated({AddView.class}) OperationLogEntity entityName) throws Exception{
        
        return operationLogConfigServicesImpl.insertSelective(entityName);
    }
    /**
    * 更新
    * @param entityName
    * @return 成功失败
    * @throws Exception
    */
    @PostMapping(value = "/update")
    @ApiOperation(value = "系统日志"+ BaseMsg.MODULAR_UPDATE,notes = "系统日志"+ BaseMsg.MODULAR_ADD)
    @Log(detail = "系统日志"+ BaseMsg.MODULAR_UPDATE,operationType = OperationType.UPDATE)
    public ResultMsg updateOperationLog(@Validated({UpdateView.class}) OperationLogEntity entityName) throws Exception{
        
        return operationLogConfigServicesImpl.updateSelectiveById(entityName);

    }
    /**
    * 根据id查询
    * @param id
    * @return 查询对象
    * @throws Exception
    */
    @GetMapping(value = "/findById")
    @ApiOperation(value = "系统日志"+ BaseMsg.MODULAR_FIND_ID,notes = "系统日志"+BaseMsg.MODULAR_FIND_ID)
    @Log(detail = "系统日志"+ BaseMsg.MODULAR_FIND_ID,operationType = OperationType.SELECT)
    public ResultMsg findOperationLogById(String id) throws Exception{
        OperationLogEntity entityName = operationLogAccessServicesImpl.selectById(id);
         return ResultMsg.success(entityName);
    }
     /**
     * 删除
     * @param ids
     * @return 成功失败
     * @throws Exception
     */
     @PostMapping(value = "/delById")
     @ApiOperation(value = "系统日志"+BaseMsg.MODULAR_DEL,notes ="系统日志"+BaseMsg.MODULAR_FIND_ID)
     @Log(detail ="系统日志"+BaseMsg.MODULAR_DEL,operationType = OperationType.DELETE)
     public ResultMsg delOperationLogById(String ids) throws Exception{
         return operationLogConfigServicesImpl.deleteById(ids);
     }
    /**
     * 计数
     * @param dto
     * @return 成功失败
     * @throws Exception
     */
    @PostMapping(value = "/countOperationLogType")
    @ApiOperation(value = "系统日志"+BaseMsg.MODULAR_DEL,notes ="系统日志"+BaseMsg.MODULAR_FIND_ID)
    @Log(detail ="系统日志"+BaseMsg.MODULAR_DEL,operationType = OperationType.DELETE)
    public ResultMsg countOperationLogType(OperationLogDTO dto, String chartType) throws Exception{
        Map<String,String> map = new HashMap<>();
        map.put("operationType","=");
        map.put("describeLog","=");
        return operationLogAccessServicesImpl.countByTableColumns(TABLE_NAME, COLUMNS_NAME,MyStrRegex.sqlWhere(map,dto),chartType);
    }
    /**
     * 求和
     * @param dto
     * @return 成功失败
     * @throws Exception
     */
    @PostMapping(value = "/sumOperationLogType")
    @ApiOperation(value = "系统日志"+BaseMsg.MODULAR_DEL,notes ="系统日志"+BaseMsg.MODULAR_FIND_ID)
    @Log(detail ="系统日志"+BaseMsg.MODULAR_DEL,operationType = OperationType.DELETE)
    public ResultMsg sumOperationLogType(OperationLogDTO dto, String chartType) throws Exception{
        Map<String,String> map = new HashMap<>();
        map.put("operationType","=");
        map.put("describeLog","=");
        Map<String,String> col = new HashMap<>();
        col.put(SUM_COL,"运行时间");
        return operationLogAccessServicesImpl.sumByTableColumns(TABLE_NAME, COLUMNS_NAME,col,MyStrRegex.sqlWhere(map,dto),chartType);
    }





}